package com.yanxu.impl;

import com.yanxu.IFileCopy;

import java.io.*;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2025-05-30-2025
 * @Description: 使用 bio 进行文件拷贝
 */
@SuppressWarnings("all")
public class BioFileCopy {
    public static void main(String[] args) throws IOException {
        BioFileCopy bioFileCopy = new BioFileCopy();
        //bioFileCopy.copyByBio();
        bioFileCopy.copyByBio_buffer();
    }

    public void copyByBio() throws IOException {
        // 使用bio, 一个字节一个字节的拷贝
        IFileCopy fileCopy = new IFileCopy() {
            @Override
            public void copyFile(File source, File target) throws IOException {
                FileInputStream inputStream = null;
                FileOutputStream outputStream = null;
                try {
                    inputStream = new FileInputStream(source);
                    outputStream = new FileOutputStream(target);
                    // 一个字节一个字节的读取。
                    // 最终读到结尾的时候，返回值为 -1
                    int result;
                    while ((result = inputStream.read()) != -1) {
                        outputStream.write(result);
                        outputStream.flush();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                    if (outputStream != null) {
                        outputStream.close();
                    }

                }
            }
        };
        fileCopy.copyFile(new File("F:\\1111.zip"), new File("F:\\11112.zip"));
    }

    public void copyByBio_buffer() throws IOException {
        // 使用bio, 借助缓冲区的方式进行文件拷贝
        IFileCopy bufferFileCopy = new IFileCopy() {
            @Override
            public void copyFile(File source, File target) throws IOException {
                BufferedInputStream bufferedInputStream = null;
                BufferedOutputStream bufferedOutputStream = null;
                try {
                    bufferedInputStream = new BufferedInputStream(new FileInputStream(source));
                    bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(target));
                    // 使用缓冲区的方式进行读取。 这里定义的是： 一次读取1024个字节
                    byte[] buffer = new byte[1024];
                    int result;
                    while ((result = bufferedInputStream.read(buffer)) != -1) {
                        bufferedOutputStream.write(buffer, 0, result);
                        bufferedOutputStream.flush();
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } finally {
                    if (bufferedInputStream != null) {
                        bufferedInputStream.close();
                    }
                    if (bufferedOutputStream != null) {
                        bufferedOutputStream.close();
                    }
                }
            }
        };
        bufferFileCopy.copyFile(new File("F:\\1111.zip"), new File("F:\\11113.zip"));
    }

}